package com.cresch.ical.downloader.ical;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 
 * @author Resch Christian
 *
 */
public class Calendar {
	
	private ArrayList<VCalendar> vCalendarList;

	private ArrayList<String> calendarStringList;
	private int amount;
	private int calendarIndex;
	private VEvent currentEvent;
	private int eventIndex;

	public Calendar() {
		vCalendarList = new ArrayList<VCalendar>();
		amount = 0;
		calendarIndex = -1;
		eventIndex = -1;
		currentEvent = null;
	}
	
	public void Build(String rawCalendar) {
		Splitter splitter = new Splitter();
		
		splitter.Initialize(VCalendar.STARTTAG, VCalendar.ENDTAG);
		calendarStringList = splitter.splittUp(rawCalendar);
		
		for (int i = 0; i < calendarStringList.size(); i++) {
			String calendarElement = calendarStringList.get(i);	
			vCalendarList.add(CalendarMapper.stringToCalendar(calendarElement));
		}		
		
		this.amount = vCalendarList.size();
		if (this.amount > 0) {
			this.stepNextCalendar();
		}
	}
	
	public void stepNextCalendar() {
		this.calendarIndex++;
		
		if (this.calendarIndex >= this.amount) {
			this.calendarIndex = this.amount-1;
		}
		
		setNextEvent();
	}
	
	public VEvent getCurrentEvent() {
		return this.currentEvent;
	}
	
	public void setNextEvent() {
		this.eventIndex++;
		if (this.eventIndex >= this.vCalendarList.get(this.calendarIndex).orderdEventList.size())
		{
			this.eventIndex = 0;
		}		
		this.currentEvent = this.vCalendarList.get(this.calendarIndex).orderdEventList.get(this.eventIndex);		
	}
	
	public void setPrevEvent() {
		if (this.eventIndex <= 0)
		{
			this.eventIndex = this.vCalendarList.get(this.calendarIndex).orderdEventList.size();
		}
		this.eventIndex--;
		this.currentEvent = this.vCalendarList.get(this.calendarIndex).orderdEventList.get(this.eventIndex);		
	}
	
	public int size() {
		return amount;
	}
}
